//
//  KLPullToRefreshTableViewController.m
//  keylime-iphone
//
//  Created by Jesse Curry on 7/17/10.
//  Copyright 2010 Circonda, Inc. All rights reserved.
//

#import "KLPullToRefreshTableViewController.h"

@implementation KLPullToRefreshTableViewController

- (void)dealloc
{
	[refreshHeaderView release];

	[super dealloc];
}

#pragma mark -
#pragma mark API
- (void)showReloadAnimationAnimated: (BOOL)animated
{
	reloading = YES;
	[refreshHeaderView toggleActivityView: YES];

	if ( animated )
	{
		[UIView beginAnimations: nil context: NULL];
		[UIView setAnimationDuration: 0.2];
		self.tableView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f,
		                                               0.0f);
		[UIView commitAnimations];
	}
	else
	{
		self.tableView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f,
		                                               0.0f);
	}
}

- (void) reloadTableViewDataSource
{
	NSLog(@"Please override reloadTableViewDataSource");
}

- (void)dataSourceDidFinishLoadingNewData
{
	reloading = NO;
	[refreshHeaderView flipImageAnimated: NO];
	[UIView beginAnimations: nil context: NULL];
	[UIView setAnimationDuration: 0.3];
	[self.tableView setContentInset: UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
	[refreshHeaderView setStatus: ViewStatusPullToReload];
	[refreshHeaderView toggleActivityView: NO];
	[UIView commitAnimations];
}

#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
	[super viewDidLoad];

	refreshHeaderView = [[KLRefreshTableHeaderView alloc] initWithFrame:
	                     CGRectMake(0.0f,
	                                0.0f - self.view.bounds.size.height,
	                                320.0f,
	                                self.view.bounds.size.height)];
	[self.tableView addSubview: refreshHeaderView];
	self.tableView.showsVerticalScrollIndicator = YES;
}

- (void)didReceiveMemoryWarning
{
	// Releases the view if it doesn't have a superview.
	[super didReceiveMemoryWarning];

	// Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
	// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
	// For example: self.myOutlet = nil;
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView: (UITableView*)tableView
{
	// Return the number of sections.
	return 1;
}

- (NSInteger)tableView: (UITableView*)tableView numberOfRowsInSection: (NSInteger)section
{
	// Return the number of rows in the section.
	return 1;
}

// Customize the appearance of table view cells.
- (UITableViewCell*)tableView: (UITableView*)tableView
       cellForRowAtIndexPath: (NSIndexPath*)indexPath
{
	static NSString* CellIdentifier = @"Cell";

	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
	if ( cell == nil )
	{
		cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
	}

	// Configure the cell...

	return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView: (UITableView*)tableView didSelectRowAtIndexPath: (NSIndexPath*)indexPath
{
	[tableView deselectRowAtIndexPath: indexPath animated: YES];
}

#pragma mark -
#pragma mark UIScrollView Overrides
- (void)scrollViewWillBeginDragging: (UIScrollView*)scrollView
{
	if ( !reloading )
	{
		checkForRefresh = YES;  //  only check offset when dragging
	}
}

- (void)scrollViewDidScroll: (UIScrollView*)scrollView
{
	if ( reloading ) return;

	if ( checkForRefresh )
	{
		if ( refreshHeaderView.isFlipped
		     && scrollView.contentOffset.y > -65.0f
		     && scrollView.contentOffset.y < 0.0f
		     && !reloading )
		{
			[refreshHeaderView flipImageAnimated: YES];
			[refreshHeaderView setStatus: ViewStatusPullToReload];
			// Play pop sound
		}
		else if ( !refreshHeaderView.isFlipped
		          && scrollView.contentOffset.y < -65.0f )
		{
			[refreshHeaderView flipImageAnimated: YES];
			[refreshHeaderView setStatus: ViewStatusReleaseToReload];
			// Play psst sound
		}
	}
}

- (void)scrollViewDidEndDragging: (UIScrollView*)scrollView
       willDecelerate: (BOOL)decelerate
{
	if ( reloading ) return;

	if ( scrollView.contentOffset.y <= -65.0f )
	{
		if ( [self.tableView.dataSource respondsToSelector:
		      @selector(reloadTableViewDataSource)] )
		{
			[self showReloadAnimationAnimated: YES];
			// Play psst2 sound
			[self reloadTableViewDataSource];
		}
	}
	checkForRefresh = NO;
}

@end

